package com.nn.smart.bus.commons.tools.utils;


import com.nn.smart.bus.commons.tools.constant.NumConstant;

import java.math.BigDecimal;
import java.time.*;
import java.util.Date;

/**
 * @author nanning
 * @date 2019/12/13 15:05
 */
public class LocalDateUtils {


    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    public static YearMonth dateToYearMonth(Date date) {
        LocalDate localDate = dateToLocalDate(date);
        return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
    }

    public static LocalTime dateToLocalTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalTime();
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    public static Date localDateToDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }


    /**
     * 计算两个时间间隔小时数
     *
     * @param start 早的时间点
     * @param end   晚的时间点
     * @param scale 保留几位小数
     * @return java.math.BigDecimal
     * @author nanning
     * @date 2020/4/3 17:27
     */
    public static BigDecimal durationHours(Date start, Date end, int scale) {
        return durationHours(dateToLocalDateTime(start), dateToLocalDateTime(end), scale);
    }

    public static BigDecimal durationMinutes(Date start, Date end, int scale) {
        return durationMinutes(dateToLocalDateTime(start), dateToLocalDateTime(end), scale);
    }

    /**
     * {@link LocalDateUtils#durationHours(Date, Date, int)}
     *
     * @param start
     * @param end
     * @param scale
     * @return java.math.BigDecimal
     * @author nanning
     * @date 2020/4/3 17:33
     */
    public static BigDecimal durationHours(LocalDateTime start, LocalDateTime end, int scale) {
        // 两个时间的间隔，早的时间点在前，晚的再后，否则为负数
        Duration duration = Duration.between(start, end);
        // 间隔多少分钟，
        long minutes = duration.toMinutes();
        return BigDecimal.valueOf(minutes).divide(BigDecimal.valueOf(NumConstant.SIXTY), scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal durationMinutes(LocalDateTime start, LocalDateTime end, int scale) {
        // 两个时间的间隔，早的时间点在前，晚的再后，否则为负数
        Duration duration = Duration.between(start, end);
        // 间隔多少分钟，
        long minutes = duration.toMinutes();
        return BigDecimal.valueOf(minutes);
    }

}
